package com.jk.vehicle.mini.controller;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.jk.vehicle.mini.config.MineConfig;
import com.jk.vehicle.mini.schedule.components.MileageStatistics;
import com.jk.vehicle.core.controller.BaseController;
import com.jk.vehicle.core.entity.ResponseEntity;
import com.jk.vehicle.mini.service.CorpService;
import com.jk.vehicle.mini.util.SpringContextUtils;
import com.jk.vehicle.mini.vo.ScanBicycleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

@Api(value = "数据初始化接口服务", tags = "数据初始化接口服务")
@RestController
@RequestMapping("/init")
public class initController extends BaseController {
    @Resource
    private MileageStatistics mileageStatistics;

    @Resource
    private MineConfig mineConfig;

    @Resource
    private CorpService corpService;

    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    /**
     * 按照日期初始化车辆的年月日的行程统计信息√
     * @param secretCode
     * @param date
     * @param corpId
     * @return
     */
    @ApiOperation("按照日期初始化车辆的年月日的行程统计信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "secretCode", value = "调用密码"),
            @ApiImplicitParam(name = "corpId", value = "厂家ID", example = "2021-01-01"),
            @ApiImplicitParam(name = "date", value = "设定时间", example = "2021-01-01"),
    })
    @PutMapping("/init_bicycle_mileage_statistics")
    private ResponseEntity initBicycleMileageStatistics(String secretCode, String date, Long corpId){
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        if (corpId == -1) {
            return failResult("厂家id不能等于-1");
        }
        String dsName = corpService.selectDsNameById(corpId);
        if(!mineConfig.getWxSecret().equals(secretCode)){
            return failResult("非法访问！");
        }
        try {
            Date someDate = DateUtils.parseDate(date, "yyyy-MM-dd");
            mileageStatistics.dayMileageStatistics(someDate, dsName);
            mileageStatistics.weekMileageStatistics(someDate, dsName);
            mileageStatistics.moonMileageStatistics(someDate, dsName);
        }catch (ParseException e){
            return failResult("初始化统计数据时，出现日期转换异常：" + e.getMessage());
        }

        // 首先查询车辆id及其最早统计时间
        return successResult();
    }

    /**
     * 通知小程序端重新加载数据源
     */
    @ApiOperation("通知小程序端重新加载数据源")
    @GetMapping("/initDynamicDataSource")
    public ResponseEntity initDynamicDataSource(){
        DynamicDataSourceProvider dynamicDataSource = (DynamicDataSourceProvider) SpringContextUtils.getBean("dynamicDataSourceProvider");
        dynamicDataSource.loadDataSources();
        try {
            dynamicRoutingDataSource.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return successResult();
    }

}